package com.lyp.qczp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.lyp.qczp.dao.Employeedao;
import com.lyp.qczp.entity.Employee;

@Service
public class EmployeeServiceImp implements EmployeeService{

	@Autowired
	private Employeedao employeedao;

	@Override
	public int save(Employee employee) {
		// TODO Auto-generated method stub
		Employee save = employeedao.save(employee);
		if (save!=null) {
			return 1;
		}
		return 0;
	}

	@Override
	public int deleteById(Integer eId) {
		employeedao.deleteById(eId);
		return 1;
	}

	@Override
	public Map<String, Object> findAll(PageRequest pageable,Employee employee) {
		Page<Employee> findAll = employeedao.findAll(selectquanpage(employee),pageable);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", findAll.getContent());
		map.put("total", findAll.getTotalElements());
		return map;
	}


	//条件查询Specification
		private Specification<Employee> selectquanpage(Employee employee){
			return new Specification<Employee>() {

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
					// TODO Auto-generated method stub
					Predicate conjunction = cb.conjunction();
					List<Expression<Boolean>> expressions = conjunction.getExpressions();
					if (employee.getEYongchu()!=null&&employee.getEYongchu()!=0) {
						expressions.add(cb.equal(root.<Integer>get("eYongchu"), employee.getEYongchu()));
					}
					if (employee.getEOrgyi()!=null&&employee.getEOrgyi()!=0) {
						expressions.add(cb.equal(root.<Integer>get("eOrgyi"), employee.getEOrgyi()));
					}
					if (employee.getEOrger()!=null&&employee.getEOrger()!=0) {
						expressions.add(cb.equal(root.<Integer>get("eOrger"), employee.getEOrger()));
					}
					if (employee.getEOrgsan()!=null&&employee.getEOrgsan()!=0) {
						expressions.add(cb.equal(root.<Integer>get("eOrgsan"), employee.getEOrgsan()));
					}
					
					return conjunction;
				}
			};
		}
	
	
	
	@Override
	public Employee findByeName(String eName) {
		// TODO Auto-generated method stub
		return employeedao.findByeName(eName);
	}

	@Override
	public int updateeTypeByeId(Integer eType, Integer eId) {
		// TODO Auto-generated method stub
		return employeedao.updateeTypeByeId(eType, eId);
	}
	
}
